const { DataTypes } = require("sequelize");

const requelize = require("../db/sequelize");

const Classify = require("./classify.model");

const Resource = requelize.define(
  "db_resource",
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      unique: true,
      primaryKey: true,
      comment: "ID",
    },
    added_by: {
      type: DataTypes.STRING(64),
      allowNull: true,
      comment: "添加人ID",
    },
    classify_id: {
      type: DataTypes.STRING(64),
      allowNull: true,
      comment: "资源所属的分类ID",
    },
    title: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: "名称",
    },
    link: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: "链接",
    },
    thumbnail: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "缩略图",
    },
    description: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: "描述",
    },
    rich_text: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: "富文本",
    },
  },
  {
    tableName: "db_resource",
    comment: "资源表",
  }
);

Resource.belongsTo(Classify, {
  foreignKey: "classify_id",
  targetKey: "id",
  as: "classifyInfo",
});
// 注释掉的代码块：这里原本是一个同步数据库表结构的调用，使用时需要解除注释并谨慎操作，因为它会强制同步表结构，可能导致数据丢失
// Resource.sync({ force: true });

// 导出用户模型，供其他模块使用
module.exports = Resource;
